﻿﻿<form id="frmBXJGWorkOrderCategory" class="frm-container">
    <input type="hidden" name="id" value="0" />
    <table class="frm-box">
        <tr>
            <th>上级</th>
            <td><input name="parentId" value="0" /></td>
        </tr>
        <tr>
            <th>名称</th>
            <td><input name="displayName" class="easyui-textbox" tabindex="0" data-options="required:true,validType:'length[0,50]'" /></td>
        </tr>
    </table>
</form>
<script>
    (function () {
        var m = 'BXJGWorkOrderCategory';
        var $dlg = $('#dlg' + m);
        var $frm = $('form');
        var $cbParent = $frm.find("[name='parentId']");
        var id = $dlg.data('id');
        var parent = $dlg.data('parent');
        

        var initParentTask = $.Deferred();
        var initFormTask = $.Deferred();

        //初始化父节点下拉框
        $cbParent.combotree({
            url: abp.appPath + 'api/services/bxjgworkorder/WorkOrderCategoryProvider/GetTreeForSelect',
            queryParams: { forType: 4, parentText: '全部' },
            prompt: abp.localization.bxjgUtils('全部'),
            onLoadSuccess: function (node, data) {
                initParentTask.resolve();
            },
            loadFilter: function (data, parent) {
                data = $.fn.combotree.defaults.loadFilter(data, parent);
                if (data && data[0])
                    data[0].id = 0;//奇怪这里用'0' 与输入口默认值对应不起来
                return data;
            }
        });
      

        $dlg.data('clickSave', function () {
            if (!$frm.form('validate'))
                return;
            let postData = $frm.serializeJSON(/*{ checkboxUncheckedValue: "false" }*/);

            
            let defer;
            if (id) {
                defer = abp.services.bxjgworkorder.workOrderCategory.update(postData).done(function (d) {
                    abp.notify.success(abp.localization.bxjgUtils('修改') + abp.localization.bxjgUtils('成功') + '!');
                });
            } else {
                postData.id = 0;
                defer = abp.services.bxjgworkorder.workOrderCategory.create(postData).done(function (d) {
                    abp.notify.success(abp.localization.bxjgUtils('新增') + abp.localization.bxjgUtils('成功') + '!');
                });
            }
            defer.done(function () {
                onSaved();
            });
            abp.ui.setBusy($dlg, defer);
        });

        function onSaved() {
            var f = $dlg.data('onSaved');
            if (f)
                f();
        }

        //没必要等下拉加载完再赋值，因为easyui会在加载完后自动处理已存在的值，其它控件也一样
        if (id) {
            initFormTask = abp.services.bxjgworkorder.workOrderCategory.get({ id: id }).done(function (d) {
                $frm.form('load', d);
            });
        } else {
            $cbParent.combotree('setValue', parent);
            initFormTask.resolve();
        }

        abp.ui.setBusy($dlg, $.when(initFormTask, initParentTask));

    })();
</script>
